
function TodoCtrl($scope, $rootScope) {
  $scope.todos = [
    {text:'learn angular', done:true},
    {text:'build an angular app', done:false}];

  $scope.addTodo = function() {
    $scope.todos.push({text:$scope.todoText, done:false});
    $scope.todoText = '';
  };

  $scope.remaining = function() {
    var count = 0;
    angular.forEach($scope.todos, function(todo) {
      count += todo.done ? 0 : 1;
    });
    return count;
  };

  $scope.archive = function() {
    var oldTodos = $scope.todos;
    $scope.todos = [];
    angular.forEach(oldTodos, function(todo) {
      if (!todo.done) $scope.todos.push(todo);
    });
  };
  
  
  
  $scope.$on('searchbox', function(event, parameters) {
	  
	  console.log("LOG: %s", 'received SearchText : ' , parameters );
	  
      $scope.todoText = parameters.searchText;
      $scope.addTodo();
      
  });

  
  $scope.$on('dropdownItem', function(event, parameters) {
	  
	  console.log("LOG: %s", 'Dropdown Clicked : ' , parameters );
     
  });

  
  
  
}



function SearchboxCtrl($scope, $rootScope) {

	  $scope.searchText = "";
	  
	  $scope.doSearch = function(elementId) {
		  
		  console.log("LOG: %s", 'Search Term : ' , elementId );
		  
		  $rootScope.$broadcast('searchbox', { source: elementId, searchText: $scope.searchText});
		  
	  };
  
}


function DropdownCtrl($scope, $rootScope) {


	  $scope.onClicked = function(elementId) {
		  
		  //console.log("LOG: %s", 'Search Term : ' , elementId );
		  
		  $rootScope.$broadcast('dropdown', { source: elementId });
		  
	  };
	
	
	  $scope.onItemClicked = function(elementId) {
		  
		  //console.log("LOG: %s", 'Search Term : ' , elementId );
		  
		  $rootScope.$broadcast('dropdownItem', { source: elementId });
		  
	  };

}


function GridMenuCtrl($scope, $rootScope) {

      $scope.selectedId = 0;
	
      $scope.$on('gridItemSelected', function(event, parameters) {
    	  
    	  console.log("LOG: %s", 'GridSelected : ' , parameters );
    	  
    	  $scope.selectedId = parameters.value;
      });    
      
}


function GridCtrl($scope, $rootScope, $http) {

      $scope.selectedId = 0;
	
      $scope.objects = [];
      
	  $scope.onGridItemSelected = function(elementId) {
		  
		  console.log("LOG: %s", 'onGridItemSelected : ' , elementId );
		  
		  $rootScope.$broadcast('gridItemSelected', { source: elementId , value: elementId});
		  
	  };
	  
	  
	  $scope.loadObjects = function(url) {

		  console.log("LOG: loadObjects : %s"  , url );
		  
		  
		  $http.get(url).success(function(data, status, headers, config) {
			    // this callback will be called asynchronously
			    // when the response is available
			  $scope.objects = data;
			  $scope.selectedId = data[0].id;
			  
		  });
		  
		  
		  //$rootScope.$broadcast('gridItemSelected', { source: elementId , value: $scope.selectedId});
		  
	  };

		  
}

